//
//  ViewController.m
//  SocialCell
//
//  Created by student10 on 2020/11/22.
//  Copyright © 2020 student10. All rights reserved.
//

#import "HomeViewDemo.h"
#import "SocialTagListView.h"
#import "SocialCell.h"
#import "Masonry.h"
#import "AppDelegate.h"
#import "CommentViewController.h"
#import "PublishViewController.h"
#import <AFNetworking.h>
#import "AppDelegate.h"

@interface HomeViewDemo () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) NSMutableArray<NSString *> *objects;
@property (nonatomic, strong) NSMutableArray * tagList;
@property (nonatomic, strong) NSMutableArray * userName;
@property (nonatomic, strong) NSMutableArray<NSArray *> *imageURL;
@property (nonatomic, strong) NSMutableArray *LikeNum;
@property (nonatomic, strong) NSMutableArray *CommentNum;
@property (nonatomic, strong) NSMutableArray *Time;
@property (nonatomic, strong) NSMutableArray *contentID;

@end

@implementation HomeViewDemo

- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSLog(@"currentMode1 = %@", [NSRunLoop currentRunLoop].currentMode);
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    /*
     *设置发布内容按钮
     */
    UIBarButtonItem *addNewContent = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addClicked)];
    self.navigationItem.rightBarButtonItem = addNewContent;
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectZero];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    // 步骤1：
    _tableView.rowHeight = UITableViewAutomaticDimension;
    // 步骤2：
    _tableView.estimatedRowHeight = 100.0;
    [self.view addSubview:_tableView];

    if (@available(iOS 11.0, *)) {
        // https://www.jianshu.com/p/b616bea9da88
        _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentAlways;
    }

    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - UITableViewDelegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    //NSLog(@"currentMode2 = %@", [NSRunLoop currentRunLoop].currentMode);
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    AppDelegate *myDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
    CommentViewController *VC = [[CommentViewController alloc] initWithContentID:self.contentID[indexPath.row] andUserID:myDelegate.selfID];
    [VC setHidesBottomBarWhenPushed:YES];
    [self.navigationController pushViewController:VC animated:YES];
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.objects.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellID = @"CellID";
    SocialCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil) {
        NSMutableArray *tagList = [NSMutableArray array];
        for(id i in self.tagList[indexPath.row]){
            NSString *temp = [[NSString alloc]initWithFormat: @"%@", i];
            //NSLog(@"%@", temp);
            [tagList addObject:temp];
        }
        AppDelegate *myDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
        cell = [[SocialCell alloc] initWithPicnum: [self.imageURL[indexPath.row] count] andCanEdit:self.canEdit andHasLike:[myDelegate.haveLikeContentList containsObject:self.contentID[indexPath.row]] andTagList:tagList];
    }
    cell.contentID = self.contentID[indexPath.row];
    [cell.avatarButton setImage:[UIImage imageNamed:@"an.jpg"] forState:UIControlStateNormal];
    cell.nicknameLabel.text = self.userName[indexPath.row];
    
    //时间戳转时间的方法
    //NSLog(@"time %@", [self.Time[indexPath.row] class]);
    NSString *timeTemp = [[NSString alloc]initWithFormat: @"%@", self.Time[indexPath.row]];
    cell.timeLabel.text = [self TimeProcessing:timeTemp];

    //cell.timeLabel.text = @"11.11";
    
    cell.contentLabel.text = self.objects[indexPath.row];
//    ((UIImageView*) cell.picArray[0]).image = [UIImage imageNamed:@"an.jpg"];
//    ((UIImageView*) cell.picArray[1]).image = [UIImage imageNamed:@"an.jpg"];
//    ((UIImageView*) cell.picArray[2]).image = [UIImage imageNamed:@"an.jpg"];
//    ((UIImageView*) cell.picArray[3]).image = [UIImage imageNamed:@"an.jpg"];
//    ((UIImageView*) cell.picArray[4]).image = [UIImage imageNamed:@"an.jpg"];
    int count = 0;
    for(id i in self.imageURL[indexPath.row]){
        NSString *temp = [[NSString alloc]initWithFormat: @"http://172.18.178.56/api/thumb/%@", i];
        NSURL *imageURL = [NSURL URLWithString:temp];
        NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
        ((UIImageView*) cell.picArray[count]).image = [UIImage imageWithData:imageData];
        count++;
    }

//
//    ((UILabel*)cell.commentlabel[0]).text = self.objects[4];
//    ((UILabel*)cell.commentlabel[1]).text = self.objects[5];
//    ((UILabel*)cell.commentlabel[2]).text = self.objects[6];
    
    NSString *temp = [[NSString alloc]initWithFormat: @"%@", self.LikeNum[indexPath.row]];
    cell.likeCountLabel.text = temp;
    temp = [[NSString alloc]initWithFormat: @"%@", self.CommentNum[indexPath.row]];
    cell.commentCountLabel.text = temp;
    //cell.commentlabel.text = @"aaaaaaaaaaaa";
    
    return cell;
}

- (NSString *) TimeProcessing: (NSString *)timeString{
    
    NSString *timestamp = timeString;
    NSString *timestamp2 = [timestamp stringByReplacingOccurrencesOfString:@"000" withString:@""];
    NSLog(@"%@",timestamp2);
    NSDate *timeData = [NSDate dateWithTimeIntervalSince1970:[timestamp2 intValue]];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy年MM月dd日 HH:mm"];
    NSString *strTime = [dateFormatter stringFromDate:timeData];
    return strTime;
}

- (void)sss:(id)sender {
    //NSLog(@"sssss");
}

- (void)InitData:(NSMutableArray *)detail tagList:(NSMutableArray *)tagList userName:(NSMutableArray *)userName image:(NSMutableArray<NSArray *> *)image LikeNum:(NSMutableArray *)LikeNum CommentNum:(NSMutableArray *)CommentNum Time:(NSMutableArray *)Time ContentID:(NSMutableArray *)contentID
{
    self.objects = detail;
    self.tagList = tagList;
    self.userName = userName;
    self.imageURL = image;
    self.LikeNum = LikeNum;
    self.CommentNum = CommentNum;
    self.Time = Time;
    self.contentID = contentID;
    self.canEdit = YES;
}

- (void)addClicked
{
    [self.navigationController pushViewController:[[PublishViewController alloc] init] animated:YES];
}

- (void) getSelfContentText{
    AppDelegate *myDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
    NSString *url = [NSString stringWithFormat:@"http://172.18.178.56/api/content/texts/self"];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager GET:url parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [self AnalysiDictionary:responseObject];
        [self creatContent];
        [self InitData:myDelegate.selfcontent tagList:myDelegate.selfTag userName:myDelegate.selfUserNames image:nil LikeNum:myDelegate.selfLikeNum CommentNum:myDelegate.selfCommentNum Time:myDelegate.selfTime ContentID:myDelegate.selfContentID];
        [self.tableView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        //NSLog(@"请求失败--%@",error);
    }];
}

- (void) AnalysiDictionary:(NSDictionary *)Dictionary{
    AppDelegate *myDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
    NSArray * arr1 = Dictionary[@"Data"];
    myDelegate.selfNames = [NSMutableArray array];\
    myDelegate.selfDetail = [NSMutableArray array];
    myDelegate.selfTag = [NSMutableArray array];
    myDelegate.selfUserNames = [NSMutableArray array];
    myDelegate.selfLikeNum = [NSMutableArray array];
    myDelegate.selfCommentNum = [NSMutableArray array];
    myDelegate.selfTime = [NSMutableArray array];
    myDelegate.selfContentID = [NSMutableArray array];
    for(NSDictionary * dic in arr1){
        [myDelegate.selfDetail addObject:dic[@"Detail"]];
        [myDelegate.selfNames addObject:dic[@"Name"]];
        [myDelegate.selfTag addObject:dic[@"Tag"]];
        [myDelegate.selfLikeNum addObject:dic[@"LikeNum"]];
        [myDelegate.selfCommentNum addObject:dic[@"CommentNum"]];
        [myDelegate.selfUserNames addObject:myDelegate.selfName];
        [myDelegate.selfTime addObject:dic[@"PublishDate"]];
        [myDelegate.selfContentID addObject:dic[@"ID"]];
    }
}

- (void) creatContent{
    AppDelegate *myDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
    myDelegate.selfcontent = [NSMutableArray array];
    for(int i = 0; i < [myDelegate.selfNames count]; i++){
        NSString *temp = [[NSString alloc]initWithFormat: @"%@\n%@", myDelegate.selfNames[i], myDelegate.selfDetail[i]];
        [myDelegate.selfcontent addObject: temp];
    }
}

- (void) removeObjectFromIndexpath:(NSIndexPath *)indexPath
{
    [self.objects removeObjectAtIndex:indexPath.row];
    [self.tagList removeObjectAtIndex:indexPath.row];
    [self.userName removeObjectAtIndex:indexPath.row];
    [self.imageURL removeObjectAtIndex:indexPath.row];
    [self.LikeNum removeObjectAtIndex:indexPath.row];
    [self.CommentNum removeObjectAtIndex:indexPath.row];
    [self.Time removeObjectAtIndex:indexPath.row];
    [self.contentID removeObjectAtIndex:indexPath.row];
}

@end

